package genedit.view.graph;

import genedit.model.graph.GraphElement;
import genedit.model.graph.Point;
import genedit.model.graph.classModel.GeneralizationLink;
import genedit.model.graph.classModel.InterfaceModel;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.util.ArrayList;

public class GeneralizationLinkPainter extends LinkPainter {

	public GeneralizationLinkPainter(GraphElement element) {
		super(element);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public void paint(Graphics2D g) {
		//super.paint(g);
		GeneralizationLink link = (GeneralizationLink) element;
		Color backupColor=g.getColor();
		g.setColor(link.getColor());
		Stroke backupStroke = g.getStroke();
		Paint backupPaint = g.getPaint();
		if ((link.getEnd() == null) || (link.getEnd().getParent() instanceof InterfaceModel)) {
			g.setStroke(new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0,
					new float[] { 2, 2 }, 0));
		}
		ArrayList<Point> points = link.getAllPoints();
		// ako zelimo da se icrtavaju sve linije ovaj faktor je 0
		int factor = 0;
		if (link.getEnd() != null)
			factor = 1;
		for (int i = 0 + factor; i < points.size() - 1 - factor; i++) {
			g.drawLine((int) points.get(i).getPosition().getX(), (int) points.get(i).getPosition()
					.getY(), (int) points.get(i + 1).getPosition().getX(), (int) points.get(i + 1)
					.getPosition().getY());
		}
		for (Point p : points) {
			// preskoci crtanje pocetnih tacaka
			if (p.equals(link.getStart()) || p.equals(link.getEnd()))
				continue;
			g.fillRect((int) p.getPosition().getX() - rectDim, (int) p.getPosition().getY()
					- rectDim, rectEdge, rectEdge);
		}

		// ako je veza zavrsena crtaj strelice
		link = (GeneralizationLink) getElement();
		if (link.getEnd() != null && link.getBreakPoints().size() > 1) {
			AffineTransform backup = g.getTransform();
			AffineTransform newTrans = new AffineTransform(backup);
			Point breakPoint;
			
			// strelica na pocetku
			if(link.getStartNavigable()){
				
			}
			
			// strelica na kraju
			if(link.getEndNavigable()){
				newTrans = new AffineTransform(backup);
				breakPoint = points.get(points.size() - 2);
				newTrans.translate(breakPoint.getPosition().getX(), breakPoint.getPosition().getY());
				newTrans.rotate(getAngleOfLineBetweenTwoPoints(breakPoint,
						points.get(points.size() - 3)));
				g.setTransform(newTrans);
				g.setStroke(new BasicStroke(2f));
				GeneralPath p0 = new GeneralPath();
			      p0.moveTo(0.0f, 0.0f);
			      p0.lineTo(-10.0f, 10.0f);
			      p0.lineTo(10.0f, 10.0f);
			      p0.closePath();
			      g.draw(p0);
			      g.setColor(Color.BLACK);
			      g.setPaint(Color.WHITE);
			      g.setPaintMode();
			      g.fill(p0);
			      g.setColor(backupColor);
			      g.setPaint(backupPaint);
			      g.setStroke(backupStroke);
			}
			g.setTransform(backup);
		}

		g.setStroke(backupStroke);
		g.setColor(backupColor);
		g.setPaint(backupPaint);
	}

}
